tongfusoft.profileDocUpdate = function() {
}

tongfusoft.profileDocUpdate.prototype = {

		init : function() {
		      $("#selectFile").html(this.selectFileHtml1);
		    		
		      $('.filestyle').filestyle({buttonText:'选择文件'});
		},
	    selectFileHtml1:"<input id='fileupload_input' type='file' data-buttonText='选择文件'  minlength='1' data-classbutton='btn btn-default' data-classinput='form-control inline' class='form-control filestyle' />",
	    selectFileHtml2:"  <div class='bootstrap-filestyle  input-group'><input class='form-control'required='' id='selectFileName' disabled type='text'><span class='input-group-btn'><button onclick=' new tongfusoft.serverFileList().openFileListPage()' type='button' class='  btn btn-default'><em class='fa fa-folder-open'></em>选择文件</button></span></div>",
		selectUploadModel:function(){
			var sfh1 = this.selectFileHtml1;
			var sfh2 = this.selectFileHtml2;
			$("input[name='uploadMode']").on("change",function(){
				$("#selectFileId").val('');
				$("#selectFile").empty();
				if($('input:radio[name="uploadMode"]:checked').val()=='1'){
					 $("#selectFile").html(sfh1);
					 $('.filestyle').filestyle({buttonText:'选择文件'});
		    	}else{
		    		 $("#selectFile").html(sfh2);
		    	}
				
			})
		},
	submit : function() {
		$.validator.setDefaults({
			submitHandler : function() {

				var formData = new FormData();

				$.map(common.getFormData("dcoForm"), function(v, k) {

					formData.append(k, v);

				});
				formData.append("docFolder",$("#selectFolderId").val());

				var docMetadata=common.getFormDataByClass("profileMetaData");
				docMetadata["uploadMode"]=$('input:radio[name="uploadMode"]:checked').val()
				if($('input:radio[name="uploadMode"]:checked').val()=='1'){
						formData.append('file', $('#fileupload_input')[0].files[0]);
				}else{
					
					docMetadata["selectFileId"]=$("#selectFileId").val();
					
				}
				
				formData.append('docMetadata',JSON.stringify(docMetadata));
				$.ajax({
					url : common.getContextPath('docUpdate/upload'),
					type : 'POST',
					cache : false,
					data : formData,
					processData : false,
					contentType : false,
					success : function(data) {

						if(data.state=="success"){
							
							window.location.href =common.getContextPath($("#returnPage").val());
						}else if(data.state=="error"){
							
							if(data.code=='write_power_error'){
								
								toastr["error"]('操作权限不足');
								
							}else{
								
								toastr["error"]("保存失败");
							}
						}
						
					
						
						
						
					}

				})

			}
		});
		$("#dcoForm").validate();

		$("#submitBtn").click(function() {

			$("#dcoForm").submit();
		})

	},
	dataMete : function() {

		$('.dateclass').datetimepicker({
			format : 'YYYY-MM-DD HH:mm',
			icons : {
				time : 'fa fa-clock-o',
				date : 'fa fa-calendar',
				up : 'fa fa-chevron-up',
				down : 'fa fa-chevron-down',
				previous : 'fa fa-chevron-left',
				next : 'fa fa-chevron-right',
				today : 'fa fa-crosshairs',
				clear : 'fa fa-trash'
			}
		});
	},
	selectData : function() {

		$.each($(".selectData"), function(index, item) {
			if ($("#" + $(item).attr("id") + "_data").val() != null
					|| $("#" + $(item).attr("id") + "_data").val() != '') {
				var vals = $("#" + $(item).attr("id") + "_data").val().split(",");

				$("#" + $(item).attr("id")).empty();
				var option = "<option value=''></option>";
				for (var i = 0; i < vals.length; i++) {

					option = option + "<option value='" + vals[i] + "'>" + vals[i] + "</option>";
				}
				$("#" + $(item).attr("id")).html(option);
			}
		});
	},
	returnBtn : function() {

		$("#returnBtn").click(function() {

			window.location.href =common.getContextPath($("#returnPage").val());
		})

	},
	getDocDetail : function() {

		common.post('docDetail/update/' + $("#docName").val(), {}, function(data) {

			if (data.state == 'success') {
					$.map(data.data, function(v, k) {
						$("#" + k).val(v);

					})
					
					$.map(eval('(' + data.data.docMetadata + ')'), function(v1, k1) {
						$("#" + k1).val(v1);

					})
					$("#docFile").html('<a href="'+common.getContextPath('download/'+data.data.docName)+'" target="_blank" >'+data.data.originalName+'</a>')


			}

		});

	}
}

$(function() {
	var obj = new tongfusoft.profileDocUpdate();
	obj.init();
	obj.submit();
	obj.dataMete();
	obj.selectData();
	obj.returnBtn();
	obj.getDocDetail();
	obj.selectUploadModel();
	
	var obj2=new  tongfusoft.serverFileList();
	obj2.fileList();
	obj2.selectedFile();
	obj2.openFolderPage();
	obj2.selectFolderBtn();
})